package com.hdu.hot100.tree;

public class lc230 {


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        int kth;
        int k;
        int ans = 0;
        public int kthSmallest(TreeNode root, int k) {
           this.k = k;
           kth = 0;
           f(root);
           return ans;
        }

        void f(TreeNode root)
        {
            if (root == null) return;
            f(root.left);
            kth++;
            if (kth == k)
            {
                ans = root.val;
                return;
            }
            f(root.right);
        }
    }

}
